<?php

/*
  VoIP Wholesale 1.0
  ------------------
  Document   : module_mod_settings
  Created on : Sep 25, 2011, 1:28:26 PM
  Author     : Milos Jovanovic <miloscode@gmail.com>
  Description:

 */
class Module_mod_settings extends MY_Model {
    
    function __construct() {
        parent::__construct();

		$this->MODULE->config->load('module_config');	
		$this->MODULE->short_name = $this->MODULE->config->item('short_name');
		$this->MODULE->settings_table = $this->MODULE->config->item('settings_table');	 		
    }
	
public function get_all_settings_from_db()
{
	$query = $this->db->get($this->MODULE->settings_table);
	
	foreach($query->result_array() as $entry)
	{
		$result[$entry['name']] = $entry['value'];
	}
	
	return $result;
}


public function get_setting_item_from_db($item_name)
{
	$query = $this->db->where('name',$item_name)->get($this->MODULE->settings_table);
	
	$entry = $query->row_array();
	
	$result = $entry['value'];
	
	return $result;
}

public function save_settings($post)
{	

	$data['default_company_payment_delay'] = (int)$post['default_company_payment_delay'];
	$data['default_carrier_payment_delay'] = (int)$post['default_carrier_payment_delay'];

	// only integers allowed for number of days for default delays
	if(preg_match('/[a-zA-Z]/', $post['default_company_payment_delay']))
	{
		$response = "false|wrong_default_company_payment_delay|".lang('msg_wrong_default_company_payment_delay');	
	}
    elseif(preg_match('/[a-zA-Z]/', $post['default_carrier_payment_delay']))
    {	
		$response = "false|wrong_default_carrier_payment_delay|".lang('msg_wrong_default_carrier_payment_delay');	
	}
	else
	{	
		$this->db->where('name','default_company_payment_delay')->update($this->MODULE->settings_table,array('value' => $post['default_company_payment_delay']));
		$this->db->where('name','default_carrier_payment_delay')->update($this->MODULE->settings_table,array('value' => $post['default_carrier_payment_delay']));	
		$this->db->where('name','default_company_billing_period')->update($this->MODULE->settings_table,array('value' => $post['default_company_billing_period']));
		$this->db->where('name','default_carrier_billing_period')->update($this->MODULE->settings_table,array('value' => $post['default_carrier_billing_period']));
	
		$response = "true|".lang('msg_settings_saved_successfully');
	}
	
	return $response;	
}

}
?>
